/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */


import { expect } from '@jest/globals';
import * as fse from 'fs-extra';
import * as path from 'path';

import { initByStageEmptyApp, initExecuteCommand, runHvigor, runShouldFail } from './common/common';
import { ModuleBuildProfileModifier } from './common/modifier/module-build-profile-modifier';

describe('test process resource', () => {

  test.concurrent('test stage mode process resource and incremental', async () => {
    await initByStageEmptyApp(async (projectRoot) => {
      const commands = initExecuteCommand({
        mode: 'module',
        product: 'default',
        module: 'entry',
        tasks: 'default@ProcessResource',
        log: 'd'
      });
      const result = runHvigor(projectRoot, commands);
      expect(result.stdout).toContain('Finished :entry:default@ProcessResource...');
      expect(result.stdout).toContain('restool module names: entry,feature,library,library1');
      expect(result.stdout).toContain('restool module names: feature,entry,library,library1');
      expect(result.stdout).toContain('restool module names: library,entry,feature,library1');
      expect(result.stdout).toContain('restool module names: library1,entry,feature,library');
      expect(result.stdout).toContain('BUILD SUCCESSFUL');

      const executeAgainResult = runHvigor(projectRoot, commands);
      expect(executeAgainResult.stdout).toContain('UP-TO-DATE :entry:default@ProcessResource...');
      expect(executeAgainResult.stdout).toContain('BUILD SUCCESSFUL');

      const resConfigFilePath = path.resolve(projectRoot,
        'entry/build/default/intermediates/res/default/resConfig.json');
      fse.removeSync(resConfigFilePath);

      const executeAgainResultAfterDelete = runHvigor(projectRoot, commands);
      expect(executeAgainResultAfterDelete.stdout).toContain('Finished :entry:default@ProcessResource...');
      expect(result.stdout).toContain('restool module names: entry,feature,library,library1');
      expect(result.stdout).toContain('restool module names: feature,entry,library,library1');
      expect(result.stdout).toContain('restool module names: library,entry,feature,library1');
      expect(result.stdout).toContain('restool module names: library1,entry,feature,library');
      expect(executeAgainResultAfterDelete.stdout).toContain('BUILD SUCCESSFUL');
    });
  });

  test.concurrent('test target custom resource path validate', async () => {
    await initByStageEmptyApp(async (projectRoot) => {
      const commands = initExecuteCommand({
        mode: 'module',
        product: 'default',
        module: 'entry',
        tasks: 'default@ProcessResource',
        log: 'i'
      });

      const moduleBuildProfileModifier = new ModuleBuildProfileModifier(projectRoot, 'entry');
      moduleBuildProfileModifier.updateTarget({
        name: 'default',
        resource: {
          directories: [
            'src/main/resources1'
          ]
        }
      });
      const errorResourcePath = path.resolve(projectRoot, 'src/main/resources1');
      runShouldFail(projectRoot, commands, errorResourcePath);
    });
  });
});
